<template>
  <el-dialog v-model="dialogVisible" title="设置规则">
    <el-form :model="ruleForm" label-position="top" label-width="120px">
      <el-form-item label="排名选项">
        <el-checkbox-group v-model="ruleForm.rulesList" @change="handleChangeRule">
          <el-checkbox v-for="item in ruleSetList" :label="item.value">{{ item.name
          }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="规则排序（可拖动选项进行排序）">
        <draggable :list="defaultList" ghost-class="ghost" :force-fallback="true" chosen-class="chosenClass"
          animation="300" item-key="name">
          <template #item="{ element, index }">
            <div class="rule-item cursor-move">
              {{ index + 1 }}&nbsp;&nbsp;{{ RankRuleText[element as keyof typeof RankRuleText] }}
            </div>
          </template>
        </draggable>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitRules">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, onUpdated } from "vue"
import { ruleSetList } from "../mock"
import draggable from "vuedraggable"
import { RankRuleText } from "@/views/competition/config/index"
import { RankRuleEnum } from "../../config/enum";


const props = defineProps<{
  changeRule: Function;
}>();
const dialogVisible = ref(false);
const defaultList = ref<String[]>([RankRuleEnum.TEAM_SCORES, RankRuleEnum.TOTAL_GOAL_DIFFERENCE, RankRuleEnum.TOTAL_GOAL, RankRuleEnum.INTER_OUTCOME]);
const ruleForm = reactive({
  rulesList: ref<String[]>([]),
})

// 排名选项
const handleChangeRule = () => {
  defaultList.value = ruleForm.rulesList;
}
// 设置规则
const submitRules = () => {
  props.changeRule(defaultList.value);
  dialogVisible.value = false;
}

onUpdated(() => {
  ruleForm.rulesList = defaultList.value;
  props.changeRule(defaultList.value);
})

defineExpose({
  dialogVisible,
});
</script>
<style lang="scss" scoped>
.rule-item {
  height: 30px;
  line-height: 30px;
  background: #FAFAFA;
  margin-bottom: 5px;
  padding-left: 20px;
  border-radius: 3px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
